package com.laicunba.service.impl;

import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.laicunba.service.MobileMessageService;
import com.laicunba.util.ThreadUtil;


public class MobileMessageServiceLCImpl implements MobileMessageService {
	private static final Logger LOG = Logger.getLogger(MobileMessageServiceLCImpl.class);

	@Value("${mobile.msg.lc.url}")
	private String url;
	@Value("${mobile.msg.lc.account}")
	private String account;
	@Value("${mobile.msg.lc.pswd}")
	private String pswd;
	@Value("${mobile.msg.lc.mock}")
	private boolean mock;

	public void sendMsgAsync(final String mobile, final String msg) {
		if (mock) {
			LOG.warn(String.format("短信未发送警告(由于 mock被设置为true)  手机号码:%s ,短信内容: %s", mobile, msg));
			return;
		}
		ThreadUtil.execute(new Runnable() {

			@Override
			public void run() {
				String urlpath = String.format("%s?account=%s&pswd=%s&mobile=%s&msg=%s&needstatus=true", url, account, pswd, mobile, msg);
				HttpURLConnection conn = null;
				try {
					URL url = new URL(urlpath);
					conn = (HttpURLConnection) url.openConnection();
					String content = IOUtils.toString(conn.getInputStream());
					LOG.info(String.format("发送短信完毕  手机号[%s],内容[%s],发送结果[%s]", mobile, msg, content));
				} catch (Exception e) {
					LOG.error("短信发送出错", e);
				} finally {
					conn.disconnect();
				}
			}
		});
	}

	@Override
	public void sendMsgAsyncByAlidayu(String templateCode,String mobile, JSONObject param) {
		// TODO Auto-generated method stub
		
	}

}
